package cn.knet.excel.imp.knetexceltool.thread;

import cn.knet.excel.imp.knetexceltool.DTO.KnetBgdataCompanyBase;
import cn.knet.excel.imp.knetexceltool.service.KnetBgdataCompanyBaseService;
import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.concurrent.atomic.LongAdder;

@Slf4j
public class CorpImpThread implements Runnable {

    private LongAdder global_okAdder = new LongAdder();
    private LongAdder global_failAdder = new LongAdder();
    private LongAdder single_okAdder = new LongAdder();
    private LongAdder single_failAdder = new LongAdder();
    private KnetBgdataCompanyBaseService knetBgdataCompanyBaseService;
    private File file;
    private int fileIndex;
    /**
     * excel-表头总行数  （读取时，跳过）
     */
    private int excelHeadCount = 0;

    public CorpImpThread(File file, int fileIndex, int excelHeadCount, KnetBgdataCompanyBaseService knetBgdataCompanyBaseService, LongAdder global_okAdder, LongAdder global_failAdder) {
        this.global_okAdder = global_okAdder;
        this.global_failAdder = global_failAdder;
        this.knetBgdataCompanyBaseService = knetBgdataCompanyBaseService;
        this.fileIndex = fileIndex;
        this.excelHeadCount = excelHeadCount;
        this.file = file;
    }

    @Override
    public void run() {
        long stime = System.currentTimeMillis();
        String fileName = file.getName().replaceAll(">", "_");
        log.info(String.format("******start reading  file[%s]-[%s]******", fileIndex, fileName));
        BasicExcelListener basicExcelListener = new BasicExcelListener(knetBgdataCompanyBaseService, fileName, global_okAdder, global_failAdder, single_okAdder, single_failAdder);
        EasyExcel.read(file.getPath(), KnetBgdataCompanyBase.class, basicExcelListener).headRowNumber(excelHeadCount).sheet().doRead();
        long etime = System.currentTimeMillis();
        log.info(String.format("******end reading file[%s]-[%s] total_ok_fail[%s_%s_%s] total_time[%s]秒", fileIndex, fileName, basicExcelListener.smCount, single_okAdder.sum(), single_failAdder.sum(), (etime - stime) / 1000d));
    }
}
